package behaviours;

import java.util.Iterator;

import jade.content.ContentElement;
import jade.content.lang.Codec.CodecException;
import jade.content.onto.OntologyException;
import jade.content.onto.UngroundedException;
import jade.content.onto.basic.Action;
import jade.content.onto.basic.Result;
import jade.core.ContainerID;
import jade.core.behaviours.OneShotBehaviour;
import jade.core.behaviours.SequentialBehaviour;
import jade.domain.FIPANames;
import jade.domain.JADEAgentManagement.QueryPlatformLocationsAction;
import jade.domain.mobility.MobilityOntology;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.util.leap.List;

public class MoveToMuseumBehaviour extends SequentialBehaviour {

	private static final long serialVersionUID = 340581299311572092L;

	public MoveToMuseumBehaviour() {
		addSubBehaviour(new SendLocationQueryToAMSBehaviour());
		addSubBehaviour(new ListenForAMSReplyBehaviour());
		scheduleFirst();
	}
	
	private class SendLocationQueryToAMSBehaviour extends OneShotBehaviour {

		private static final long serialVersionUID = -6203415305981975535L;

		@Override
		public void action() {
			System.out.println(myAgent.getName() + ": Querying AMS for museum location.");
			QueryPlatformLocationsAction query = new QueryPlatformLocationsAction();
			Action action = new Action(myAgent.getAID(), query);

			ACLMessage message = new ACLMessage(ACLMessage.REQUEST);
			message.addReceiver(myAgent.getAMS());
			message.setLanguage(FIPANames.ContentLanguage.FIPA_SL);
			message.setOntology(MobilityOntology.getInstance().getName());
			try {
				myAgent.getContentManager().fillContent(message, action);
			} catch (CodecException e) {
				e.printStackTrace();
			} catch (OntologyException e) {
				e.printStackTrace();
			}

			myAgent.send(message);
		}
	}
	
	private class ListenForAMSReplyBehaviour extends OneShotBehaviour {

		private static final long serialVersionUID = -7813880910397127526L;

		public void action() {
			System.out.println(myAgent.getName() + ": Listening for reply from AMS...");
			ACLMessage receivedMessage = myAgent
					.blockingReceive(MessageTemplate.MatchSender(myAgent
							.getAMS()));
			ContentElement content = null;
			try {
				content = myAgent.getContentManager().extractContent(
						receivedMessage);
			} catch (UngroundedException e) {
				e.printStackTrace();
			} catch (CodecException e) {
				e.printStackTrace();
			} catch (OntologyException e) {
				e.printStackTrace();
			}

			Result result = (Result) content;
			List listOfPlatforms = (List) result.getValue();
			
			Iterator<?> iter = (listOfPlatforms.iterator() instanceof Iterator<?>) ? (Iterator<?>) listOfPlatforms.iterator() : null;
			while (iter.hasNext()) {
				ContainerID next = (ContainerID) iter.next();
				if (next.getID().contains("Museum-Container")) {
					System.out
							.println(myAgent.getName()
									+ ": Found museum container, moving!");
					myAgent.doMove(next);
				}
			}

		}
	}
}
